import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import Layout from "@/layouts/MainLayout.vue";
import LoginView from "@/views/LoginView.vue";
export const routes = [
  {
    path: "/login",
    name: "Login",
    component: LoginView,
  },
  {
    path: "/auth/login",
    name: "AuthLogin",
    component: () => import("@/views/auth/login.vue"),
  },
  {
    path: "/",
    name: "Index",
    component: Layout,
    meta: {
      title: "首页",
    },
    redirect: "/home",
    children: [
      {
        path: "/home",
        name: "Home",
        component: HomeView,
        meta: {
          title: "首页",
        },
      },
    ],
  },
  {
    path: "/web-gl",
    name: "WebGL",
    component: Layout,
    meta: {
      title: "webgl",
    },
    redirect: "meta",
    children: [
      {
        path: "/web-gl/meta",
        name: "WebGLMeta",
        component: () => import("@/views/web-gl/meta/index.vue"),
        meta: {
          title: "元宇宙",
        },
      },
    ],
  },
  {
    path: "/blog",
    name: "Blog",
    component: Layout,
    meta: {
      title: "博客",
    },
    redirect: "/blog/list",
    children: [
      {
        path: "/blog/list",
        name: "BlogList",
        component: () => import("@/views/blog/list/index.vue"),
        meta: {
          title: "博客列表",
        },
      },
    ],
  },
];

export default function ({ otherRoutes }) {
  const modules = import.meta.glob("../views/**/*.vue");
  editRouteConfig(otherRoutes);
  const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [...routes, ...otherRoutes],
  });
  return router;

  function editRouteConfig(otherRoutes) {
    for (let item of otherRoutes) {
      if (item.children && item.children.length > 0) {
        item.component = Layout;
        editRouteConfig(item.children);
      } else {
        item.component = modules[`../views${item.path}/index.vue`];
      }
    }
  }
}
